﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using FreedBack.Models;
using FreedBack.Helpers;
using FreedBack.Core.Entities;
using Freedback.Core.Repositories;
using Freedback.Core.Context;
using Freedback.Core;
using FreedBack.WebInfrastructure;

namespace FreedBack.Controllers
{
    public partial class HomeController : Controller
    {
        private readonly IGenericRepository _repository;
        private readonly IUnitOfWork _unitOfWork;
        private readonly IWebRequest _request;

        public HomeController(IGenericRepository repository, IUnitOfWork unitOfWork, IWebRequest request)
        {
            _repository = repository;
            _unitOfWork = unitOfWork;
            _request = request;
        }

        public virtual ActionResult Index()
        {
            ViewBag.Message = "Welcome to Freedback!";

            return View();
        }

        public virtual ActionResult ProvideFeedback(string SourceClientUserId, string SourceUrl)
        {
            ViewBag.AllAreasOfFeedback = _repository.GetAll<AreaOfFeedback>().ToList().Select(i => new SelectListItem { Text = i.Name, Value = i.Id.ToString() }).GetWithEmptyFirstItem();
            ViewBag.AllRatingValues = _repository.GetAll<RatingValue>().ToList().Select(i => new SelectListItem { Text = i.Name, Value = i.Id.ToString() }).GetWithEmptyFirstItem();
            var feedbackForm = new FeedbackFormModel { SourceClientUserId = string.IsNullOrWhiteSpace(SourceClientUserId) ? _request.UserHostAddress : SourceClientUserId, SourceUrl = string.IsNullOrWhiteSpace(SourceUrl) ? _request.RawUrl : SourceUrl };
            return View(feedbackForm);
        }

        [HttpPost]
        public ActionResult ProvideFeedback([Bind(Exclude = "FeedbackFormId, CreatedDate")] FeedbackFormModel feedbackform)
        {
            ViewBag.AllAreasOfFeedback = _repository.GetAll<AreaOfFeedback>().ToList().Select(i => new SelectListItem { Text = i.Name, Value = i.Id.ToString() }).GetWithEmptyFirstItem();
            ViewBag.AllRatingValues = _repository.GetAll<RatingValue>().ToList().Select(i => new SelectListItem { Text = i.Name, Value = i.Id.ToString() }).GetWithEmptyFirstItem();
            if (ModelState.IsValid)
            {
                _repository.Add<FeedbackForm>(new FeedbackForm
                {
                    AreaOfFeedbackId = feedbackform.AreaOfFeedbackId,
                    AreaOfFeedbackOther = feedbackform.AreaOfFeedbackOther,
                    Comments = feedbackform.Comments,
                    CreatedDate = TimeFactory.Now(),
                    FeedbackTypeId = feedbackform.FeedbackTypeId,
                    RatingValueId = feedbackform.RatingValueId,
                    SourceClientUserId = feedbackform.SourceClientUserId,
                    SourceUrl = feedbackform.SourceUrl
                });
                _unitOfWork.Save();
                return RedirectToAction("Index");
            }
            else
            {
                return View(feedbackform);
            }
        }
    }
}
